package edu.nepu.flink.api.window;

import edu.nepu.flink.api.bean.WaterSensor;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;

/**
 * @Date 2024/2/29 21:49
 * @Created by chenshuaijun
 */
public class AggregateWindow {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        SingleOutputStreamOperator<WaterSensor> source = env.socketTextStream("hadoop102", 9999).map(new MapFunction<String, WaterSensor>() {
            @Override
            public WaterSensor map(String value) throws Exception {
                String[] split = value.split(",");
                return new WaterSensor(split[0], Long.valueOf(split[1]), Integer.valueOf(split[2]));
            }
        });
        /**
         * 有了reduce为什么还需要aggregate：
         * 因为reduce传入的参数需要数据的输入类型、中间类型和输出类型都必须相同。这个就限制了计算的灵活性
         * 而aggregate方法的输入、中间结果和输出类型完全可以是不同的
         */
        source.keyBy(WaterSensor::getId)
                        .window(TumblingProcessingTimeWindows.of(Time.seconds(10)))
                        .aggregate(new AggregateFunction<WaterSensor, Integer, String>() {
                            /**
                             * 这个是初始化累加器的方法，因为我们的累加器是Integer类型的，所以初始化值可以设置为0
                             * @return
                             */
                            @Override
                            public Integer createAccumulator() {
                                return 0;
                            }

                            /**
                             * 这个是累加的方法
                             * @param value The value to add
                             * @param accumulator The accumulator to add the value to
                             * @return
                             */
                            @Override
                            public Integer add(WaterSensor value, Integer accumulator) {
                                return accumulator + value.getVc();
                            }

                            /**
                             * 这个方法是返回累计器的结果
                             * @param accumulator The accumulator of the aggregation
                             * @return
                             */
                            @Override
                            public String getResult(Integer accumulator) {
                                return String.valueOf(accumulator);
                            }

                            /**
                             * 这个方法一般是用不到的，只有会话窗口才会使用到这个方法
                             * @param a An accumulator to merge
                             * @param b Another accumulator to merge
                             * @return
                             */
                            @Override
                            public Integer merge(Integer a, Integer b) {
                                return null;
                            }
                        }).print();

        env.execute();
    }
}
